package com.zbkj.crmeb.store.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.enums.BannerJumpTypeEnum;
import com.zbkj.crmeb.store.dao.StoreBannerSetUpDao;
import com.zbkj.crmeb.store.model.StoreBannerSetUp;
import com.zbkj.crmeb.store.model.StoreModuleSetUp;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductDescription;
import com.zbkj.crmeb.store.request.StoreBannerSetUpRequest;
import com.zbkj.crmeb.store.response.StoreBannerSetUpResponse;
import com.zbkj.crmeb.store.service.StoreBannerSetUpService;
import com.zbkj.crmeb.store.service.StoreModuleSetUpService;
import com.zbkj.crmeb.store.service.StoreProductDescriptionService;
import com.zbkj.crmeb.store.service.StoreProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class StoreBannerSetUpServiceImpl extends ServiceImpl<StoreBannerSetUpDao, StoreBannerSetUp> implements StoreBannerSetUpService {
    @Resource
    private StoreBannerSetUpDao storeBannerSetUpDao;
    @Autowired
    StoreProductService storeProductService;
    @Autowired
    private StoreProductDescriptionService storeProductDescriptionService;

    /**
     * 分页查询
     *
     * @param @param request pageParamRequest
     * @return PageInfo<StoreBannerSetUpResponse>
     * @Description:
     * @author duyb
     * @Date: 2022-10-27
     */
    @Override
    public PageInfo<StoreBannerSetUpResponse> pageEntity(StoreBannerSetUpRequest request, PageParamRequest pageParamRequest) {
        Page<Object> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<StoreBannerSetUp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getBannerName())) {
            lambdaQueryWrapper.like(StoreBannerSetUp::getBannerName, request.getBannerName());
        }
        if (request.getIsShow() != null) {
            lambdaQueryWrapper.eq(StoreBannerSetUp::getIsShow, request.getIsShow());
        }
        lambdaQueryWrapper.orderByAsc(StoreBannerSetUp::getSort);
        lambdaQueryWrapper.orderByAsc(StoreBannerSetUp::getCreateTime);
        List<StoreBannerSetUp> storeBannerSetUpList = this.list(lambdaQueryWrapper);
        List<StoreBannerSetUpResponse> storeBannerSetUpResponseList = new ArrayList<>();

        //处理JSONObject
        if (CollectionUtils.isNotEmpty(storeBannerSetUpList)) {
            for (StoreBannerSetUp storeBannerSetUp : storeBannerSetUpList) {
                StoreBannerSetUpResponse storeBannerSetUpResponse = new StoreBannerSetUpResponse();
                BeanUtils.copyProperties(storeBannerSetUp, storeBannerSetUpResponse);
                storeBannerSetUpResponse.setJumpObject(JSON.parseObject(storeBannerSetUp.getJumpObject()));
                storeBannerSetUpResponse.setType(storeBannerSetUp.getType().getCode());
                storeBannerSetUpResponseList.add(storeBannerSetUpResponse);
            }
        }

        return CommonPage.copyPageInfo(page, storeBannerSetUpResponseList);
    }

    /**
     * 通过主键查询
     *
     * @param @param id
     * @return StoreBannerSetUpResponse
     * @Description:
     * @author duyb
     * @Date: 2022-10-27
     */
    @Override
    public StoreBannerSetUpResponse getEntity(Integer id) {
        StoreBannerSetUp storeBannerSetUp = this.getById(id);
        if (null == storeBannerSetUp) {
            return null;
        }
        StoreBannerSetUpResponse storeBannerSetUpResponse = new StoreBannerSetUpResponse();
        BeanUtils.copyProperties(storeBannerSetUp, storeBannerSetUpResponse);
        storeBannerSetUpResponse.setType(storeBannerSetUp.getType().getCode());
        storeBannerSetUpResponse.setJumpObject(JSON.parseObject(storeBannerSetUp.getJumpObject()));
        JSONObject jsonObject = JSON.parseObject(storeBannerSetUp.getJumpObject());
        //跳转类型为商品
        if (storeBannerSetUp.getType().getCode() == 1) {
            StoreProduct product = storeProductService.getById(jsonObject.getString("productId"));
            //获取商品图片
            StoreProductDescription storeProductDescriptions = storeProductDescriptionService.getOne(Wrappers.<StoreProductDescription>lambdaQuery()
                    .eq(StoreProductDescription::getProductId, product.getId())
                    .last("limit 1")
            );
            product.setImage(storeProductDescriptions.getImage());
            storeBannerSetUpResponse.setProduct(product);
        }

        return storeBannerSetUpResponse;
    }


    /**
     * 新增/修改
     *
     * @param @param storeBannerSetUpRequest
     * @Description:
     * @author duyb
     * @Date: 2022-10-27
     */
    @Override
    public Boolean saveEntity(StoreBannerSetUpRequest storeBannerSetUpRequest) {
        //banner名称不能重复
        StoreBannerSetUp one = this.getOne(Wrappers.<StoreBannerSetUp>lambdaQuery()
                .eq(StoreBannerSetUp::getBannerName, storeBannerSetUpRequest.getBannerName())
                .last("limit 1")
        );
        if (ObjectUtil.isNotNull(one) && !Objects.equals(one.getId(), storeBannerSetUpRequest.getId())) {
            throw new CrmebException("banner名称已存在");
        }

        //banner最多启用8张
        out:if (storeBannerSetUpRequest.getIsShow()) {
            if (ObjectUtil.isNotNull(storeBannerSetUpRequest.getId())){
                StoreBannerSetUp bannerSetUp = this.getById(storeBannerSetUpRequest.getId());
                if (bannerSetUp.getIsShow()){
                    break out;
                }
            }
            Integer number = storeBannerSetUpDao.selectCount(Wrappers.<StoreBannerSetUp>lambdaQuery()
                    .eq(StoreBannerSetUp::getIsShow, true)
            );
            if (number == 8) {
                throw new CrmebException("同时最多可展示8张banner!");
            }
        }
        if (StringUtils.isBlank(storeBannerSetUpRequest.getBannerName())) {
            throw new CrmebException("banner名称不能为空!");
        }

        if (storeBannerSetUpRequest.getType() == null) {
            throw new CrmebException("跳转类型不能为空!");
        }
        if (CollectionUtils.isEmpty(storeBannerSetUpRequest.getJumpObject())) {
            throw new CrmebException("跳转对象不能为空!");
        }
        if (StringUtils.isBlank(storeBannerSetUpRequest.getImage())) {
            throw new CrmebException("banner图片不能为空!");
        }
        if (Objects.equals(storeBannerSetUpRequest.getType(), 3) && StringUtils.isBlank(storeBannerSetUpRequest.getBgImage())) {
            throw new CrmebException("背景图片不能为空!");
        }
        //排序默认999
        if (storeBannerSetUpRequest.getSort() == null) {
            storeBannerSetUpRequest.setSort(999);
        }else if(storeBannerSetUpRequest.getSort()>999){
            throw new CrmebException("排序号最大为999!");
        }
        if (null == storeBannerSetUpRequest.getId()) {
            storeBannerSetUpRequest.setCreateTime(new Date());
        }
        StoreBannerSetUp storeBannerSetUp = new StoreBannerSetUp();
        BeanUtils.copyProperties(storeBannerSetUpRequest, storeBannerSetUp);
        storeBannerSetUp.setType(BannerJumpTypeEnum.valueOf(storeBannerSetUpRequest.getType()));
        storeBannerSetUp.setJumpObject(storeBannerSetUpRequest.getJumpObject().toJSONString());

        storeBannerSetUp.setUpdateTime(new Date());

        return this.saveOrUpdate(storeBannerSetUp);
    }

    /**
     * 启用/禁用
     *
     * @param @param id
     * @return Boolean
     * @Description:
     * @author duyb
     * @Date: 2022-11-08
     */
    @Override
    public Boolean enabled(Integer id, Integer status) {
        //banner最多启用8张
        if (Objects.equals(status, 1)) {
            Integer number = storeBannerSetUpDao.selectCount(Wrappers.<StoreBannerSetUp>lambdaQuery()
                    .eq(StoreBannerSetUp::getIsShow, true)
            );
            if (number == 8) {
                throw new CrmebException("同时最多可展示8张banner!");
            }
        }
        boolean b = this.update(Wrappers.<StoreBannerSetUp>lambdaUpdate()
                .eq(StoreBannerSetUp::getId, id)
                .set(StoreBannerSetUp::getIsShow, status)
        );

        return b;
    }


}
